<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/31
 * Time: 11:19
 */

namespace app\shop\model;


use think\Model;

class CartModel extends Model
{
    protected $table = 'dp_cart';
    protected $field = true;
    public static function addInCart($data)
    {
        return self::insert($data);
    }

    public static function getCartNum($user_id)
    {
        return self::where('user_id', $user_id)->count();
    }

    public static function goodsExist($user_id, $goods_id, $spec_key)
    {
        $where['goods_id'] = $goods_id;
        $where['user_id'] = $user_id;
        if($spec_key != 0){
            $where['spec_key'] = $spec_key;
        }
        return self::where($where)->count();
    }

    public static function addGoodsNum($user_id, $goods_id, $spec_key, $add_num)
    {
        $where['goods_id'] = $goods_id;
        $where['user_id'] = $user_id;
        if($spec_key != 0){
            $where['spec_key'] = $spec_key;
        }
        return self::where($where)->setInc('goods_num', $add_num);
    }

    public static function getCartList($user_id)
    {
        return self::where('user_id', $user_id)->select();
    }

    public static function changeItemSelected($id, $selected)
    {
        return self::where('id', $id)->update(['selected' => $selected]);
    }

    public static function changeItemNum($id, $num)
    {
        return self::where('id', $id)->update(['goods_num' => $num]);
    }
}